//////////////////////////////////////////////////////////////////////////
///	\file:	WindowTimer.cpp
///	Copyright (MingYou) 2009 - All Rights Reserved
///	Author:	Robert.Ma
///	Date:	[2009/04/25]
///	Description:
///		A timer class in a window.
//////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "WindowTimer.h"
#include "CETool.h"

//////////////////////////////////////////////////////////////////////////
/// \fn		CWindowTimer::CWindowTimer(HWND hWindow, DWORD dwInterval, BOOL bAutoSet)
/// \brief	Constructor of CWindowTimer.
CWindowTimer::CWindowTimer(DWORD dwInterval, BOOL bAutoSet)
		: m_hWindow(NULL),
		m_dwInterval(dwInterval),
		m_bSet(bAutoSet)
{
	if ( bAutoSet )
	{
		Start();
	}
}

//////////////////////////////////////////////////////////////////////////
/// \fn		CWindowTimer::~CWindowTimer(void)
/// \brief	Destructor of CWindowTimer.
CWindowTimer::~CWindowTimer(void)
{
	try
	{
		Stop();
	}
	catch (...)
		{}
}

//////////////////////////////////////////////////////////////////////////
/// \fn		void __stdcall CWindowTimer::TimerProc(HWND, UINT, UINT uID, DWORD)
/// \brief	time procedure.
void __stdcall CWindowTimer::TimerProc(HWND, UINT, UINT uID, DWORD)
{
	( (CWindowTimer*)uID )->PerformEvent();
}

//////////////////////////////////////////////////////////////////////////
/// \fn		DWORD __fastcall CWindowTimer::GetInterval(void) const
/// \brief	get interval.
DWORD __fastcall CWindowTimer::GetInterval(void) const
{
	return m_dwInterval;
}

//////////////////////////////////////////////////////////////////////////
/// \fn		UINT CWindowTimer::Start()
/// \brief	Set timer.
UINT CWindowTimer::Start()
{
	if ( !(::IsWindow(m_hWindow)) )
	{
		m_hWindow = CETool::GetPlazaWindow();

		ASSERT(::IsWindow(m_hWindow));
	}

	UINT uRes = ::SetTimer(m_hWindow, (UINT)this, (UINT)m_dwInterval,
	                       (TIMERPROC)CWindowTimer::TimerProc);

	if ( 0 != uRes )
	{
		m_bSet = TRUE;
	}

	return uRes;
}

//////////////////////////////////////////////////////////////////////////
/// \fn		BOOL CWindowTimer::Stop()
/// \brief	Stop timer.
BOOL CWindowTimer::Stop()
{
	if ( m_bSet )
	{
		m_bSet = ::KillTimer(m_hWindow, (UINT)this);
	}

	return m_bSet;
}
